package com.cz.zslu.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.cz.zslu.common.ReturnObject;
import com.cz.zslu.entity.ZslBanner;
import com.cz.zslu.entity.ZslBrowseHistory;
import com.cz.zslu.interceptor.BaseContextHandler;
import com.cz.zslu.rest.BaseController;
import com.cz.zslu.service.IZslBrowseHistoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 历史浏览 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/sys/browseHistory")
@Api(description = "历史浏览管理")
public class BrowseHistoryController extends BaseController<IZslBrowseHistoryService, ZslBrowseHistory> {

    @ApiOperation("插入浏览记录")
    @RequestMapping(value = "/saveBrowseHistory", method = RequestMethod.POST)
    public ReturnObject<List<ZslBrowseHistory>> saveBrowseHistory(@ApiParam("商铺id") Long storeId,
                                                                  @ApiParam("商品id") Long commodityId) {

        ZslBrowseHistory info = new ZslBrowseHistory();
        info.setCommodityId(commodityId);
        info.setStoreId(storeId);
        baseService.insert(info);

        return new ReturnObject<>(0, "插入成功", null);
    }

    @ApiOperation("获取当前用户浏览分页")
    @RequestMapping(value = "/getMyBrowseHistoryPage", method = RequestMethod.POST)
    public ReturnObject<List<ZslBrowseHistory>> getMyBrowseHistoryPage(@ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
                                                                       @ApiParam("页数") @RequestParam(defaultValue = "1") int offset) {

        EntityWrapper<ZslBrowseHistory> ew = new EntityWrapper<>();
        ew.eq("user_id", BaseContextHandler.getUserID());
        ew.orderBy("crt_time", false);
        Page<ZslBrowseHistory> obj = baseService.selectPage(new Page<>(offset, limit), ew);

        return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());
    }


    @ApiOperation("获取全部浏览分页")
    @RequestMapping(value = "/getBrowseHistoryPage", method = RequestMethod.POST)
    public ReturnObject<List<ZslBrowseHistory>> getBrowseHistoryPage(@ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
                                                                     @ApiParam("页数") @RequestParam(defaultValue = "1") int offset) {

        EntityWrapper<ZslBrowseHistory> ew = new EntityWrapper<>();
        ew.orderBy("crt_time", false);
        Page<ZslBrowseHistory> obj = baseService.selectPage(new Page<>(offset, limit), ew);

        return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());
    }


}

